﻿<cfscript>
/** 
* DataBase Frameworks - Sequence
* 
* @hint "DataBase Frameworks - Sequence - Questionnaire" 
*/ 

component 
	displayname="public.service.db.senate.sequence.Questionnaire" 
	output=false 
	accessors=true 
	{

	/**
	* @hint "初始化对象"
	*/
	public function init() output=false {
		return this;
	}
	
	/**
	* @hint "自定义主键规则"
	*/
	public string function getID() output=false {
		
		var nextID = year(now()) & numberFormat(1, "0___");

		var sql = "	SELECT
						q.qsn_id
					FROM
						t_questionnaire q
					WHERE
						q.qsn_id LIKE :condition 
					ORDER BY
						q.qsn_id DESC";

		var queryObj = new Query(datasource = application.dnsMaster, maxRows = 1);
		queryObj.addParam(name = "condition", value = year(now()) & "____", cfsqltype = "cf_sql_char" );
		var rs_check = queryObj.execute(sql=sql).getResult();

		/* 
			主键生成规则 
			
			4位年份 + 4位流水号
			
		*/

		if( rs_check.recordCount ){
			/* 创建一个追加id */
			nextID = year(now()) & numberFormat((right(rs_check.qsn_id, 4) + 1), "0___");
			
		}

		return nextID;
	}

}

</cfscript>